'use strict';

const chalk = require('chalk');
const exec = require('shelljs').exec;
const child = exec('git diff --cached --name-only', {
    cwd: process.cwd(),
    silent: true
});
if (child.code) {
    process.exit(1);
} else {
	const modifyFiles = child.stdout.toString().split('\n');
	const disSubmitFiles = [];
    modifyFiles.forEach(function (file) {
        inExcludeFile(file) && disSubmitFiles.push(file);
    });
	if(disSubmitFiles.length){
		disSubmitFiles.forEach((file) => {
			console.log(`${chalk.red('禁止提交文件')} ${chalk.green(file)}`);
			console.log(`${chalk.red('您可以使用')} ${chalk.green(`git reset -- ${file}`)} ${chalk.red('撤销暂存区')}\n`);			
		});
		process.exit(1);
	}
}

function inExcludeFile(file) {
	const exclude = [/^core\//i];
	for (let i = 0; i < exclude.length; i++) {
		if (exclude[i].test(file)) {
			return true;
		}
	}
	return false;
}
